/**
 * 兑换码详情页面
 */
Ext.define("PRO.shop.view.cdkey.shopCDKeyInfoView",{
    extend: 'Ext.panel.Panel',
    layout:'column',
    alias: 'widget.shop.shopcdkeyinfoview',
    border:0,
    autoScroll : true,
    initComponent: function(){
        var me = this;
        var arr = [];
        var data = me.getData(me.vals.number);
        Ext.each(data.products,function(itemPanel,index){
            arr.push({
                xtype:'box',
                data:itemPanel,
                style:'float:left;',
                tpl:new Ext.XTemplate(
                    '<div>',
                        '<div>',
                            '<div style="display:inline-block; border: 2px solid #F2F2F2;justify-content: center;align-items: center;flex-direction: row;height: 190px;width: 200px;padding: 5px;text-align: center;margin: 10px 28px;">',
                                '<div style="height: 60%;background: #ECF0F5;margin-bottom:10px;">',
                                    '<img style="display:block;width:100%;"src="{imageCover}"/>',
                                '</div>',
                                '<div style="display: flex;flex-direction: row;justify-content: center;align-items: center;height: 40%;">',
                                    // '<i class="jeicon jeicon-check-circle" style="color:#A8E63A;margin-right: 15px;"></i>',
                                    '<div>',                  
                                        '<p style="font-size:20px;">{productName}</p>',
                                        '<p style="font-size:14px;color:#999999;">有效时长<font style="color:#000000;margin-left: 15px;"><tpl if="amount!==0">{amount}人*</tpl>{timeLength}{timeLengthUnitName}</font></p>',
                                    '</div>',
                                '</div>',
                            '</div>',
                        '</div>',
                    '</div>'
                )
                // listeners:{
                //     afterrender:function(view){
                //         view.getEl().on('click',function(){
                //             var hasClass = view.getEl().dom.getAttribute('class');
                //             if(hasClass.split(' ').includes('selected-exchange')){
                //                 view.getEl().dom.setAttribute('class','not-selected-exchange x-component x-component-default');
                //             }else{
                //                 view.getEl().dom.setAttribute('class','selected-exchange x-component x-component-default');
                //             }
                //         })
                //     }
                // }
            })
        })
        me.items = {
            xtype:'panel',
            layout:'column',
            columnWidth:1,
            items:[
                {
                    columnWidth:1,
                    xytpe:'panel',
                    items:arr,
                    border:false,
                    style:'overflow:hidden',
                },
                {
                    xytpe:'dataview',
                    columnWidth:1,
                    height:60,
                    margin:'0 0 30 0',
                    border:false,
                    html:'<div class="exchange"><p style="width:50%;float:left;">兑换码<span style="color:#000000;margin-top:10px;">'+data.subVoucherCreateDate+'</span></p><p style="width:50%;float:left;text-align:right;">兑换码编号<span style="color:#000000;margin-top:10px;">'+data.subVoucherNo+'</span></p></div>'
                }
            ],
            border:false
        };
        me.bbar = {
            xtype:'button',
            cls : 'btn JEPLUS_B_L10',
            margin:'10px 300px',
            text:'确认使用',
            // width:140,
            height:40,
            handler:function(btn){
                //JE.msg('确认使用!');
                JE.ajax({
                    url:'/je/shoppingmall/voucher/activeSubVoucher',
                    params:{
                        subVoucherNo:me.vals.number 
                    },
                    success:function(res){
                        var res = JSON.parse(res.responseText)
                        if(res.success){
                            JE.alert('恭喜兑换成功!');
                            btn.up('window').close();
                        }else{
                            JE.alert(res.massage)
                        }
                    }
                })
                
            }
        }
        me.callParent(arguments);
    },
    getData:function(subVoucherNo){
        var data = JE.ajax({
            url:'/je/shoppingmall/voucher/queryBindingProduct',
            params:{
                subVoucherNo:subVoucherNo 
            }
        })
        var product = {
            products :[]
        };
        if(data.success){
            Ext.each(data.obj.products,function(productItem){
                product.products.push({
                    amount: productItem.amount,
                    chargeType: productItem.chargeType,
                    coefficient: productItem.coefficient,
                    discountPrice:productItem.discountPrice,
                    imageCover:JE.getFileUrl(productItem.imageCover.split('*')[1]), 
                    // productDesp: "有效时长    1年",
                    productId: productItem.productId,
                    productName:productItem.productName,
                    productPayType:productItem.productPayType,
                    productPrice:productItem.productPrice,
                    productUnit:productItem.productUnit,
                    productUnitPrice:productItem.productUnitPrice,
                    schemeId:productItem.schemeId,
                    timeLength:productItem.timeLength,
                    timeLengthUnitCode:productItem.timeLengthUnitCode,
                    timeLengthUnitName:productItem.timeLengthUnitName
                })
            });
            product.subVoucherCreateDate = data.obj.subVoucherCreateDate;
            product.subVoucherNo = data.obj.subVoucherNo;
            return product
        }else{
            JE.alert(data.massage)
        }
    }
});